<?php
/**
* @version		$Id: helper.php 2010-07-15 vinaora $
* @package		VINAORA SLICK SLIDESHOW
* @copyright	Copyright (C) 2010 VINAORA. All rights reserved.
* @license		GNU/GPL
* @website		http://vinaora.com
* @email		admin@vinaora.com
* 
*/

// no direct accessd
defined( '_JEXEC' ) or die( 'Restricted access' );

require_once dirname(__FILE__).DS."helper".DS."vinaora_item.php";
require_once dirname(__FILE__).DS."helper".DS."vinaora_param.php";

class modVinaoraSlickshowHelper
{
	/*
	 * Get content of the config file
	 */
	function getConfig($path, $name){

		$path = JPath::clean($path);
		$path = $path.DS.$name;

		if (file_exists($path)){
			$xml = &JFactory::getXMLParser( 'simple' );
			$xml->loadFile($path);
			
			//TODO File exits but not valid XML
			return $xml;
		}
		else{
			//TODO File not exits
		}

		return NULL;

	}
	
	/*
	 * Create the config file
	 */
	function createConfig($path, $name, $xml){

		jimport('joomla.filesystem.file');
		$path = JPath::clean($path);

		if (is_writeable($path)){
			if ( JFile::write($path.DS.$name, $xml) ) return true;
			else{
				// TODO: Write file error
			}
		}
		else{
			// TODO: Folder is not writeable
		}

		return false;

	}

	/*
	 * Create content of the config file
	 */
	function createXML($params){
		// Create Element - <banner>
		$node = new JSimpleXMLElement('banner');
		
		// Create General Settings
		$node = &modVinaoraSlickshowHelper::createGeneral($node, $params);
		
		// Create Items
		$node = &modVinaoraSlickshowHelper::createItems($node, $params);
		
		$xml = '<?xml version="1.0" encoding="utf-8"?>';
		$xml .= $node->toString(true);
		
		$xml = str_replace('slideshowtime>', 'slideShowTime>', $xml);

		return $xml;
	}
	
	
	/*
	 *
	 */
	function &createGeneral(&$node, $params){
	
		$attbs = "width|height|backgroundColor|backgroundTransparency|startWith|barHeight|fadeTransition|verticalTransition|controllerTop|transitionSpeed|titleX|titleY";
		$attbs = explode("|", $attbs);
		foreach ($attbs as $attb){
			$node->addAttribute($attb, $params->get($attb));
		}
		
		
		return $node;
	}
	
	/*
	 * Create Items
	 */
	function &createItems(&$node, $params){
		// Create Element - <banner>.<items>
		$nodeL1 = &$node->addChild('items');
		$items = explode("\n", $params->get('item_path'));
		for ($i=1; $i<=count($items); $i++){
			$nodeL1 = &modVinaoraSlickshowHelper::createItem($nodeL1, $params, $i);
		}
		
		return $node;
	}
	
	/*
	 * Create a Item Element
	 */
	function &createItem(&$node, $params, $position=1){
		// Create Element - <banner>.<items>.<item>
		$nodeL1 = &$node->addChild('item');
		
		// Create Element - <banner>.<items>.<item>.<title>
		$param	= $params->get('item_title');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		$nodeL2	= &$nodeL1->addChild('title');
		if (!empty($param)){
			$nodeL2->setData($param);
		}
		
		// Create Element - <banner>.<items>.<item>.<path>
		$param	= $params->get('item_path');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		$nodeL2	= &$nodeL1->addChild('path');
		if (!empty($param)){
			$nodeL2->setData($param);
		}

		// Create Element - <banner>.<items>.<item>.<url>
		$param	= $params->get('item_url');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		$nodeL2	= &$nodeL1->addChild('url');
		if (!empty($param)){
			$nodeL2->setData($param);
		}

		// Create Element - <banner>.<items>.<item>.<target>
		$param	= $params->get('item_target');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		$nodeL2	= &$nodeL1->addChild('target');
		if (!empty($param)){
			$param	= extVinaoraParam::validTarget($param);
			$nodeL2->setData($param);
		}
		
		// Create Element - <banner>.<items>.<item>.<bar_color>
		$param	= $params->get('item_bar_color');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		if (empty($param)){
			$param	= $params->get("barColor");
		}
		else{
			$param	= extVinaoraParam::validColor($param, substr($params->get("barColor"), -6, 6), "0x");
		}
		
		$nodeL2	= &$nodeL1->addChild('bar_color');
		$nodeL2->setData($param);
		
		// Create Element - <banner>.<items>.<item>.<bar_transparency>
		$param	= $params->get('item_bar_transparency');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		
		if (empty($param)){
			$param = $params->get("barTransparency");
		}
		else{
			$param	= extVinaoraParam::validTransparency($param);
		}
		
		$nodeL2	= &$nodeL1->addChild('bar_transparency');
		$nodeL2->setData($param);
			
		// Create Element - <banner>.<items>.<item>.<slideShowTime>
		$param	= $params->get('item_slideShowTime');
		$param	= extVinaoraParam::getParam($param, $position, "\n");
		$param	= trim($param);
		if ($param == ""){
			$param = $params->get("slideShowTime");
		}
		else{
			$param	= abs(intval($param));
		}
		$nodeL2	= &$nodeL1->addChild('slideShowTime');
		$nodeL2->setData($param);
		
		return $node;
	}
	
	/*
	 * Get List of Items from a Folder
	 */
	function getItems($folder, $fullpath=false){

		$root		= JPATH_BASE.DS.'images'.DS.'stories';
		$default	= JPATH_BASE.DS.'media'.DS.'mod_vinaora_slickshow'.DS.'images'.DS.'default';
		
		$filter 	= '\.png$|\.gif$|\.jpg$|\.bmp$';
		$exclude	= array('index.html', '.htaccess');
		
		$items		= extVinaoraItem::getItems($folder, $root, $default, $filter, $exclude, $fullpath);
		
		return $items;

	}
	
	/*
	 * Get Item Path
	 */
	function getItemPath($folder){
		
		$root		= 'images/stories/';
		$default	= MEDIA_URL.'images/default';
		
		$path		= extVinaoraItem::getItemPath($folder, $root, $default);
		
		return $path;
		
	}
	
	/*
	 * Get List of Item URLs
	 */
	function getItemPaths($folder){
		$path 	= modVinaoraSlickshowHelper::getItemPath($folder);
		$items	= modVinaoraSlickshowHelper::getItems($folder);
		
		if (count($items)){
			foreach($items as $key=>$value){
				$items[$key] = $path.'/'.$value;
			}
		}

		return $items;
	}

}
